package hw.txtreader;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.bifan.txtreaderlib.main.TxtConfig;
import com.bifan.txtreaderlib.ui.HwTxtPlayActivity;
import com.bifan.txtreaderlib.utils.FileProvider;
import com.bifan.txtreaderlib.utils.SPUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.File;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class DemoActivity extends AppCompatActivity {
    private static final int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 0x01;
    private String FilePath = Environment.getExternalStorageDirectory() + "/test4.txt";
    private Boolean Permit = false;
    private List<Book> mBookList = new ArrayList<>();
    private Gson mGson = new Gson();
    private ListView mListView;
    private ListAdapter mListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Permit = CheckPermission();
        String bookListStr = SPUtils.getInstance().getString("bookList", "");
        if (!TextUtils.isEmpty(bookListStr)) {
            Type type = new TypeToken<ArrayList<Book>>() {
            }.getType();
            ArrayList<Book> bookList = mGson.fromJson(bookListStr, type);
            mBookList.addAll(bookList);
        }
        String filePath = SPUtils.getInstance().getString("FilePath", "");
        if (Permit && !TextUtils.isEmpty(filePath)) {
            HwTxtPlayActivity.loadTxtFile(this, filePath);
        }
        if (mBookList.isEmpty() && !TextUtils.isEmpty(filePath)) {
            Book book = new Book();
            book.path = filePath;
            try {
                book.name = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.lastIndexOf("."));
                mBookList.add(book);
            } catch (Exception e) {
            }
        }
        mListView = findViewById(R.id.list_item);
        mListAdapter = new ListAdapter(this, mBookList);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Book book = mBookList.get(i);
                loadFile(book.path);
            }
        });
        mListView.setAdapter(mListAdapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {//是否选择，没选择就不会继续
            Uri uri = data.getData();//得到uri，后面就是将uri转化成file的过程
            String path = FileProvider.getFileAbsolutePath(this, uri);
            Book book = new Book();
            book.path = path;
            try {
                assert path != null;
                book.name = path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf("."));
                mBookList.add(book);
            } catch (Exception e) {
                toast("文件出错了");
            }
            mListAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        String json = mGson.toJson(mBookList);
        SPUtils.getInstance().putString("bookList", json);
    }

    public void chooseFile(View view) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("text/plain");//设置类型
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, 3);
    }

    public void loadFile(String filePath) {
        if (Permit) {
            TxtConfig.saveIsOnVerticalPageMode(this, false);
            FilePath = filePath;
            if (TextUtils.isEmpty(FilePath) || !(new File(FilePath)).exists()) {
                toast("文件不存在");
            } else {
                SPUtils.getInstance().putString("FilePath", FilePath);
                HwTxtPlayActivity.loadTxtFile(this, FilePath);
            }
        }
    }

    public void onVerticalMode(View view) {
        if (Permit) {
//            FilePath = mEditText.getText().toString().trim();
            if (TextUtils.isEmpty(FilePath) || !(new File(FilePath)).exists()) {
                toast("文件不存在");
            } else {
                TxtConfig.saveIsOnVerticalPageMode(this, true);
                HwTxtPlayActivity.loadTxtFile(this, FilePath);
            }
        }
    }

    public void loadStr(View view) {
        if (Permit) {
//            String str = mEditText.getText().toString();
//            if (TextUtils.isEmpty(str)) {
//                toast("输入为空字符");
//            } else {
//                HwTxtPlayActivity.loadStr(this, str);
//            }
        }
    }


    public void displayMore(View view) {
        if (Permit) {
//            FilePath = mEditText.getText().toString().trim();
            if (TextUtils.isEmpty(FilePath) || !(new File(FilePath)).exists()) {
                toast("文件不存在");
            } else {
                Intent intent = new Intent(this, MoreDisplayActivity.class);
                intent.putExtra("filePath", FilePath);
                startActivity(intent);
            }
        }
    }

    private Boolean CheckPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            // 安卓11，判断有没有“所有文件访问权限”权限
            if (Environment.isExternalStorageManager()) {
                return true;
            } else {
                Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                intent.setData(Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent, MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
                return false;
            }
        }
        if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(this,
                new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS},
                MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
            return false;
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Permit = true;
                loadFile(null);
            } else {
                // Permission Denied
                Toast.makeText(DemoActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
            }
            return;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    private Toast t;

    private void toast(String msg) {
        if (t != null) {
            t.cancel();
        }
        t = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        t.show();
    }
}
